package com.show.dyvm;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.aliyun.tea.TeaException;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dyvmsapi.model.v20170525.SingleCallByTtsRequest;
import com.aliyuncs.dyvmsapi.model.v20170525.SingleCallByTtsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.show.business.common.constant.HttpStatus;
import com.show.business.common.core.domain.AjaxResult;
import com.show.business.integrate.bean.DyvmConfig;
import lombok.extern.slf4j.Slf4j;

/**
 * 阿里云 语音通话 --- 根据文本转语音模板进行语音通话
 * @author ccx
 * @date 2024/10/25
 */
@Slf4j
public class AliDyvmUtil {

    private  String accessKeyId = "你的阿里云Key";
    private  String accessKeySecret = "你的阿里云Secret";

    /**
     * 语音通话到某个用户
     * @param dyvmConfig 被叫号码
     * @return
     */
    public static AjaxResult sendSingleCallToUser(DyvmConfig dyvmConfig) {
        if(StrUtil.hasBlank(dyvmConfig.getAccessKeyId(),dyvmConfig.getAccessKeySecret()
                ,dyvmConfig.getPhoneNumbers(),dyvmConfig.getContent(),dyvmConfig.getTemplateCode())){
            return AjaxResult.error("语音服务发送参数有误");
        }
        AjaxResult result = new AjaxResult();
        try{
        //1.初始化acsClient实例 暂时不支持多region
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", dyvmConfig.getAccessKeyId(), dyvmConfig.getAccessKeySecret());
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", "Dyvmsapi", "dyvmsapi.aliyuncs.com");
        IAcsClient acsClient = new DefaultAcsClient(profile);

        //2.创建请求并设置参数
        SingleCallByTtsRequest request = new SingleCallByTtsRequest();
        //必填-被叫号码; 接收语音通知的被叫号码
        request.setCalledNumber(dyvmConfig.getPhoneNumbers());
        //发送语音通知的外呼号码CalledShowNumber
        request.setCalledShowNumber(dyvmConfig.getCalledShowNumber());
        //必填-Tts模板ID
        request.setTtsCode(dyvmConfig.getTemplateCode());
        //语音模板中的变量参数 示例：{"name":"123456","rainfall":50}
        JSONObject jsonObject = new JSONObject();
        jsonObject.putOnce("content",dyvmConfig.getContent());
        jsonObject.putOnce("time",dyvmConfig.getTime());
        request.setTtsParam(jsonObject.toString());
        //可选-音量 取值范围 0--100 默认取值 100
        request.setVolume(100);
        //可选-播放次数 默认取3
        request.setPlayTimes(3);
        //可选-语音通话的语速。取值范围为：-500~500
        request.setSpeed(5);
        //可选-外部扩展字段,此ID将在回执消息中带回给调用方
        request.setOutId(dyvmConfig.getOutId());

            //3.发送请求并获取响应
            SingleCallByTtsResponse singleCallByTtsResponse = acsClient.getAcsResponse(request);
            if(singleCallByTtsResponse.getCode() != null && singleCallByTtsResponse.getCode().equals("OK")) {
                //请求成功
                log.info("语音拨打成功！RequestId = %s , Code = %s , phone = %s",
                 singleCallByTtsResponse.getRequestId(), singleCallByTtsResponse.getCode(), dyvmConfig.getPhoneNumbers());
            }else{
                // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
                result.put(AjaxResult.CODE_TAG, HttpStatus.ERROR);
                result.put(AjaxResult.MSG_TAG,"语音拨打失败");
                result.put(AjaxResult.DATA_TAG,singleCallByTtsResponse.getMessage());
            }
        } catch (ClientException error) {
            // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            result.put(AjaxResult.CODE_TAG, HttpStatus.ERROR);
            result.put(AjaxResult.MSG_TAG,"语音拨打失败");
            result.put(AjaxResult.DATA_TAG,error.getMessage());
        }  catch (TeaException error) {
            // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            result.put(AjaxResult.CODE_TAG, HttpStatus.ERROR);
            result.put(AjaxResult.MSG_TAG,"语音拨打失败");
            result.put(AjaxResult.DATA_TAG,error.getMessage());
        } catch (Exception _error) {
            result.put(AjaxResult.CODE_TAG, HttpStatus.ERROR);
            result.put(AjaxResult.MSG_TAG,"语音拨打失败");
            result.put(AjaxResult.DATA_TAG,_error.getMessage());
        }
        return result;
    }

    public static void main(String[] args) {
        DyvmConfig dyvmConfig = new DyvmConfig();
        dyvmConfig.setAccessKeyId("LTAI5tRMyG9rxjaE7aEqcLwE");
        dyvmConfig.setAccessKeySecret("RhYhX3ng8BtTflyomgtHG0HWqR6Vfq");
        dyvmConfig.setTemplateCode("TTS_307515011");
        dyvmConfig.setContent("函谷关隧道变电房测试告警线路");
        dyvmConfig.setTime("2024-10-16 12:20:30");
        dyvmConfig.setPhoneNumbers("13140165732");
        dyvmConfig.setOutId("");
        dyvmConfig.setCalledShowNumber("02988417044");
        System.out.println(JSONUtil.toJsonStr(sendSingleCallToUser(dyvmConfig)));
    }
    /**
     * 语音通话到某个用户
     * @param phoneNumber 被叫号码
     * @param ttsParam 语音模板中的变量参数---请按模版参数有序存入
     * @param ttsCode Tts模板ID
     * @return
     */
    public SingleCallByTtsResponse sendSingleCallToUser1(String phoneNumber, String ttsCode, String ttsParam, String outId) throws ClientException {
        //1.初始化acsClient实例 暂时不支持多region
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", "Dyvmsapi", "dyvmsapi.aliyuncs.com");
        IAcsClient acsClient = new DefaultAcsClient(profile);

        //2.创建请求并设置参数
        SingleCallByTtsRequest request = new SingleCallByTtsRequest();
        //必填-被叫号码
        request.setCalledNumber(phoneNumber);
        //必填-Tts模板ID
        request.setTtsCode(ttsCode);
        //语音模板中的变量参数 示例：{"name":"123456","rainfall":50}
        request.setTtsParam(ttsParam);
        //可选-音量 取值范围 0--100 默认取值 100
        request.setVolume(100);
        //可选-播放次数 默认取3
        request.setPlayTimes(3);
        //可选-语音通话的语速。取值范围为：-500~500
        request.setSpeed(5);
        //可选-外部扩展字段,此ID将在回执消息中带回给调用方
        request.setOutId(outId);

        //3.发送请求并获取响应
        SingleCallByTtsResponse singleCallByTtsResponse = acsClient.getAcsResponse(request);
        if(singleCallByTtsResponse.getCode() != null && singleCallByTtsResponse.getCode().equals("OK")) {
            //请求成功
            log.info("processing sendCVoice success！RequestId = %s , Code = %s , phone = %s",
                    singleCallByTtsResponse.getRequestId(), singleCallByTtsResponse.getCode(), phoneNumber);
        }
        return singleCallByTtsResponse;
    }
}